# coding=utf-8

from . import business
from flask import request
from app import util, PAGE_NUM
from dbConnection.MysqlConn import Mysql
import math, urllib


# 商家购买套餐列表
@business.route('/business/recharge_list', methods=['post'])
@util.business_verify
def recharge_list(uid):
    data = {}
    data['code'] = 400
    data['count'] = 0
    data['data'] = []
    data['num'] = PAGE_NUM
    try:
        page = request.values.get('page', 1)
        c_num = (int(page) - 1) * PAGE_NUM
        sql = "select *, if(num > use_num, 0, 1) flag FROM business_pay_log WHERE bid = '%s' ORDER BY flag ASC, add_time asc limit %d, %d" % (uid, c_num, PAGE_NUM)
        sql2 = "select count(*) con FROM business_pay_log WHERE bid = '%s'" % uid
        mysql = Mysql()
        res = mysql.getAll(sql, None)
        cont = mysql.getOne(sql2, None)
        mysql.dispose()
        if res:
            for d in res:
                d['add_time'] = str(d['add_time'])
                d['recharge_price'] = str(d['recharge_price'])
                d['pay_money'] = str(d['pay_money'])
            data['code'] = 200
            data['data'] = res
        data['count'] = int(math.ceil(float(cont['con'])/float(PAGE_NUM))) if cont else 0
    except Exception as e:
        print e
        data['code'] = 500
    return util.to_json(data)


# 商家赠送记录
@business.route('/business/give_log', methods=['post'])
@util.business_verify
def give_log(uid):
    import login
    data = {}
    data['code'] = 400
    data['count'] = 0
    data['data'] = []
    data['num'] = PAGE_NUM
    try:
        page = request.values.get('page', 1)
        c_num = (int(page) - 1) * PAGE_NUM
        sql = "select g.*, r.name, u.wxNickName, c.use_num FROM business_give_log g LEFT JOIN recharge r ON r.id = g.rid " \
              "LEFT JOIN car_users u ON u.uid = g.uid LEFT JOIN user_taocan c ON c.id = g.tid WHERE g.bid = '%s' " \
              "ORDER BY g.add_time desc limit %d, %d" % (uid, c_num, PAGE_NUM)
        sql2 = "select count(*) con FROM business_give_log WHERE bid = '%s'" % uid
        mysql = Mysql()
        res = mysql.getAll(sql, None)
        cont = mysql.getOne(sql2, None)
        mysql.dispose()
        if res:
            for d in res:
                d['add_time'] = str(d['add_time'])
                d['url'] = urllib.quote(login.business_url(str(d['bid']), str(d['id'])))
            data['code'] = 200
            data['data'] = res
        data['count'] = cont['con'] if cont else 0
    except Exception as e:
        print e
        data['code'] = 500
    return util.to_json(data)


# 获取商家的套餐数
@business.route('/business/taocan', methods=['post'])
@util.business_verify
def taocan_num(uid):
    data = {}
    data['code'] = 400
    data['msg'] = '获取失败'
    try:
        sql = "select count(*) con FROM business_pay_log WHERE bid = '%s'" % uid
        mysql = Mysql()
        res = mysql.getOne(sql, None)
        mysql.dispose()
        if res is None:
            return data
        data['code'] = 200
        data['num'] = res['con'] if res else 0
        data['msg'] = '获取成功'
    except Exception as e:
        print e
        data['code'] = 500
        data['msg'] = '系统错误'
    return util.to_json(data)